package com.litidea.commons.dict;

import static com.litidea.dao.DAOKeys.TABLE_START;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.jdbc.core.RowMapper;

import com.litidea.commons.utils.CollectionUtils;
import com.litidea.dataobjects.KValueDO;

public class DynamicDictBase {

	private String id;// 代码表关键字

	private String name;// 代码表名称

	private Integer isBigNum;// 0|1

	private List<KValueDO> detailList;// 明细内容

	private Map<String, String> detailMap;// 明细内容的Map，方便取值

	private final static String GET_ONE_BIG_DATA = "select dict_big_value from "
			+ TABLE_START + "dict_details where dict_id=? and detail_id=?";

	public DynamicDictBase copyNew() {
		DynamicDictBase dictBase = new DynamicDictBase();
		dictBase.setId(id);
		dictBase.setName(name);
		dictBase.setIsBigNum(isBigNum);
		if (detailList != null) {
			List<KValueDO> tmp_detailList = new ArrayList<KValueDO>(detailList
					.size());
			Map<String, String> tmp_detailMap = new HashMap<String, String>(
					detailMap.size());
			for (KValueDO kvDO : detailList) {
				KValueDO kvDONew = new KValueDO(kvDO.getData(), kvDO.getLabel());
				tmp_detailList.add(kvDONew);
				tmp_detailMap.put(kvDO.getData(), kvDO.getLabel());
			}
			dictBase.setDetailList(tmp_detailList);
			dictBase.setDetailMap(tmp_detailMap);
		}
		return dictBase;
	}

	public List<KValueDO> copyNewDetails() {
		if (detailList != null) {
			List<KValueDO> tmp_detailList = new ArrayList<KValueDO>(detailList
					.size());
			for (KValueDO kvDO : detailList) {
				KValueDO kvDONew = new KValueDO(kvDO.getData(), kvDO.getLabel());
				tmp_detailList.add(kvDONew);
			}
			return tmp_detailList;
		}
		return null;
	}

	/**
	 * 获取大值
	 * 
	 * @param dictId
	 * @param detailId
	 * @return
	 */
	@SuppressWarnings( { "unchecked" })
	public String findBigDataByDict(String detailId) {
		List<String> list = DynamicDicts.jdbc.query(GET_ONE_BIG_DATA,
				new Object[] { id, detailId }, new RowMapper() {
					public Object mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						return rs.getString("dict_big_value");
					}
				});
		return (String) CollectionUtils.getFirstValue(list);
	}

	public String getString(String detailId) {
		if (this.isBigData()) {
			return this.findBigDataByDict(detailId);
		} else {
			return this.detailMap.get(detailId);
		}
	}

	public Integer getInteger(String detailId) {
		String value = getString(detailId);
		if (value == null)
			return null;
		return Integer.valueOf(value);
	}

	public Long getLong(String detailId) {
		String value = getString(detailId);
		if (value == null)
			return null;
		return Long.valueOf(value);
	}

	public Double getDouble(String detailId) {
		String value = getString(detailId);
		if (value == null)
			return null;
		return Double.valueOf(value);
	}

	public JSONObject getJSON(String detailId) {
		String value = getString(detailId);
		if (value == null)
			return null;
		return JSONObject.fromObject(value);
	}

	public Map<String, String> getDetailMap() {
		return detailMap;
	}

	public void setDetailMap(Map<String, String> detailMap) {
		this.detailMap = detailMap;
	}

	public DynamicDictBase() {
	}

	public DynamicDictBase(String id, String name, Integer isBigNum) {
		this.id = id;
		this.name = name;
		this.isBigNum = isBigNum;
	}

	public boolean isBigData() {
		return isBigNum != null && isBigNum == 1;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getIsBigNum() {
		return isBigNum;
	}

	public void setIsBigNum(Integer isBigNum) {
		this.isBigNum = isBigNum;
	}

	public List<KValueDO> getDetailList() {
		return detailList;
	}

	public void setDetailList(List<KValueDO> detailList) {
		this.detailList = detailList;
	}

}
